# 此文件从Python to Cobot拷贝
#用于串口通讯打开串口继电器，打开三色灯
#默认主程序运行打开绿灯，当发现未佩戴安全帽时，关闭绿灯，打开红灯；再佩戴安全帽后关闭红灯，打开绿灯
import serial

# 打开串口
if __name__ == '__main__':
    serial = serial.Serial('COM5', 9600, timeout=0.5)
    if serial.isOpen() :
        print("open success")
    else :
        print("open failed")

# 发送报文
def send(send_data):
    if (serial.isOpen()):
        # serial.write(send_data.encode('utf-8'))  # 编码
        serial.write(send_data)  # 编码
        print("发送成功", send_data)
    else:
        print("发送失败！")

# 调用发送函数
#绿灯
data_send = [0x01, 0x05, 0x00, 0x00, 0xFF, 0x00, 0x8C, 0x3A]
#红灯
data_send2 = [0x01, 0x05, 0x00, 0x01, 0xFF, 0x00, 0xDD, 0xFA]
#关绿色
data_send3 = [0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0xCD, 0xCA]
#关红色
data_send4 = [0x01, 0x05, 0x00, 0x01, 0x00, 0x00, 0x9C, 0x0A]
# send(data_send)
# send(data_send2)
# send(data_send3)
# send(data_send4)
# 01 06 00 00 00 02 08 0B
# 01 06 00 00 00 02 08 0b